package org.algorithm.wE1.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;
import org.junit.Test;

/**
 * @Date 2022/2/26
 * @Link {https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii}
 */
public class 删除排序链表中的重复元素II2 {



    @Test
    public  void test(){

        ListNode listNode = deleteDuplicates(ListNode.build(1, 2, 3, 3, 4, 4, 5));
        System.out.println(ListNode.print(listNode));

    }

    public ListNode deleteDuplicates(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        //不相同时保留当前节点 对head.next 继续进行递归
        if(head.val != head.next.val){
            //这里等同于 head=head.next
            head.next=deleteDuplicates(head.next);
            return head;
        }else {
            //相同时当前节点以及下一个节点都删掉
            ListNode next = head.next.next;
            //找到所有重复的
            while (next!=null&&head.val == next.val){
                next = next.next;
            }
            return deleteDuplicates(next);
        }

    }
}
